package com.aiti.lulian.controller;

import cn.hutool.core.util.StrUtil;
import com.aiti.base.core.base.controller.WebController;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.dto.LuLianSourcingDemandDto;
import com.aiti.lulian.entity.LuLianEnterpriseHistory;
import com.aiti.lulian.entity.LuLianSourcingDemand;
import com.aiti.lulian.service.ILuLianEnterpriseService;
import com.aiti.lulian.service.ILuLianSourcingDemandService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jsoup.helper.StringUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName LuLianSourcingDemandController
 * @Description 供需商机表接口
 * @Author diaozhende
 * @Date 2024/5/31 0031 10:52
 */
@RestController
@RequestMapping("/sourcingDemand")
public class LuLianSourcingDemandController extends WebController {

    @Resource
    private ILuLianSourcingDemandService sourcingDemandService;
    @Resource
    ILuLianEnterpriseService luLianEnterpriseService;


    /**
     * 多参数查询
     *
     * @param sourcingDemand 多参数数据
     * @return com.aiti.base.core.utils.message.Message
     * @date 2024/6/14 0014 8:48
     * @author diaozhende
     */
    @GetMapping
    public Message queryByParams(LuLianSourcingDemand sourcingDemand) {
        List<LuLianSourcingDemand> sourcingDemandList = sourcingDemandService.queryByParams(sourcingDemand);
        return Message.success(sourcingDemandList);
    }

    /**
     * 分页查询
     *
     * @param sourcingDemandDto 参数对象
     * @return com.aiti.base.core.utils.message.Message
     * @date 2024/5/31 0031 10:56
     * @author diaozhende
     */
    @GetMapping("/queryPage")
    public Message queryPage(LuLianSourcingDemandDto sourcingDemandDto) {
        Page<LuLianSourcingDemand> pageData = sourcingDemandService.queryPage(sourcingDemandDto);
        return Message.success(pageData);
    }


    /**
     * 根据id查询
     *
     * @param id ID
     * @return com.aiti.base.core.utils.message.Message
     * @date 2024/6/4 0004 10:39
     * @author diaozhende
     */
    @GetMapping("/queryById")
    public Message queryById(String id) {
        String userId = findUserId();
        if(StringUtil.isBlank(userId)){
            return Message.fail("请登陆");
        }
        LuLianSourcingDemand luLianSourcingDemand = sourcingDemandService.queryById(id,userId);
        return Message.success(luLianSourcingDemand);
    }


    /**
     * 首页新增询价信息
     *
     * @param sourcingDemand 请求参数
     * @return com.aiti.base.core.utils.message.Message
     * @date 2024/6/19 0019 15:28
     * @author diaozhende
     */
    @PostMapping("/saveDemandData")
    public Message saveDemandData(@RequestBody LuLianSourcingDemand sourcingDemand) {
        sourcingDemandService.saveDemandData(sourcingDemand);
        return Message.success();
    }


    /***
     * 保存采购询价需求信息
     * @param demand
     * @return com.aiti.base.core.utils.message.Message
     * @date 2024/06/12
     * @author yangfenzhen
     */
    @PostMapping("/addDemandData")
    public Message addDemandData(@RequestBody LuLianSourcingDemand demand) {
        String userId = findUserId();
        if(StrUtil.isNotEmpty(userId)){
            demand.setCreator(userId);
            demand.setIsContact("1");
            demand.setIsHot("1");
            LuLianEnterpriseHistory enterprise = luLianEnterpriseService.queryByCurrentUser(userId);
            if(enterprise != null){
                demand.setContacts(enterprise.getBaseName());
                demand.setPhone(enterprise.getLinkPhone());
            }
            sourcingDemandService.saveData(demand);
            return Message.success();
        }else{
            return  Message.fail("请先注册登录！");
        }

    }

    /**
     * 根据baseId查询
     *
     * @return com.aiti.base.core.utils.message.Message
     * @date 2024/7/30
     * @author WongMz
     */
    @GetMapping("/queryByBaseId")
    public Message queryByBaseId(@RequestParam("baseId") String baseId) {
        try {
            if (StrUtil.isBlank(baseId)) {
                logger.error("查询异常：===》入参异常");
                return Message.fail("入参异常");
            }
            String userId = findUserId();
            if(StringUtil.isBlank(userId)){
                return Message.fail("请登陆");
            }
            LuLianSourcingDemand luLianSourcingDemand = sourcingDemandService.queryByBaseId(baseId);
            return Message.success(luLianSourcingDemand);
        } catch (Exception e) {
            logger.error("查询异常：===》" + e);
            return Message.fail(e.getMessage());
        }
    }
}
